//
//  CheckstandViewController.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/22.
//  Copyright © 2018年 四川中农. All rights reserved.
//

enum OrderPayType {
    case alipay
    case wechat
    case payOnDelivery
    case balance
}

import UIKit

/// 收银台 和 货到付款

class CheckstandViewController: BaseViewController {
    
    @IBOutlet weak var topImageView: UIImageView!
    @IBOutlet weak var payTypeLabel: UILabel! // 支付方式
    @IBOutlet weak var withholdMoney: UILabel! // 扣款金额
    @IBOutlet weak var residueMoney: UILabel! // 剩余金额
    @IBOutlet weak var backRootBtn: UIButton!
    @IBOutlet weak var btnTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var firstButton: UIButton!
    
    var payType = OrderPayType.payOnDelivery
    
    var isBalancePay = true // 默认余额支付
    
    var isRecharge = false // 是否是充值
    
    var model:PayBalanceModel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "收银台"
        if self.isBalancePay == false {
            self.withholdMoney.isHidden = true
            self.residueMoney.isHidden = true
            self.btnTopConstraint.constant = 27
            switch self.payType {
            case .alipay:
                if self.isRecharge {
                    self.payTypeLabel.text = "已通过支付宝充值成功"
                }else{
                    self.payTypeLabel.text = "已通过支付宝支付成功"
                }
                
            case .wechat:
                if self.isRecharge {
                    self.payTypeLabel.text = "已通过微信充值成功"
                }else{
                    self.payTypeLabel.text = "已通过微信支付成功"
                }
                
            case .payOnDelivery:
                self.title = "货到付款"
                self.payTypeLabel.text = "您的货到付款订单已生成"
            default:break
            }
            if self.isRecharge {
                self.firstButton.setTitle("我的余额", for: .normal)
            }
        }else{
            self.payTypeLabel.text = "已通过余额支付成功"
            self.residueMoney.text = "剩余金额：¥" + model.userMoney
            
            let str = "本次扣除金额：¥" + model.orderMoney
            var attribute = NSMutableAttributedString.init(string: str)
            attribute.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 11)], range: NSString.init(string: str).range(of: "¥" + model.orderMoney))
            attribute = NSMutableAttributedString.init(attributedString: attribute)
            attribute.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17)], range: NSString.init(string: str).range(of: model.orderMoney.getPriceInteger()))
            
            attribute = NSMutableAttributedString.init(attributedString: attribute)
            attribute.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "#ed4848")], range: NSString.init(string: str).range(of: "¥" + model.orderMoney))
            self.withholdMoney.attributedText = attribute
        }
        
        self.view.backgroundColor = UIColor.init(hexColor: "ffffff")
        self.backRootBtn.setCornerRadio(radio: 33/2, isMask: false)
        self.backRootBtn.setBorder(color: "#f7c201", width: 1)
        self.topImageView.setCornerRadio(radio: 40, isMask: false)
        self.topImageView.setShadow(color: "#08512b,", offset: CGSize.init(width: 0, height: 4), radius: 8, opacity: 0.2)
        self.navigationItem.leftBarButtonItem = nil
        self.navigationItem.hidesBackButton = true
        
        self.setRightBarButton(title: "", imageName: "icon_kefu", action: #selector(rightButtonAction), titleColor: "")
    }
    
    @objc func rightButtonAction(){
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK:查看订单
    @IBAction func lookOverOrder(_ sender: UIButton) {
        if self.isRecharge {
            let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "MyBalanceTableViewController") as! MyBalanceTableViewController
//            ctrl.balance = self.balance
            self.navigationController?.pushViewController(ctrl, animated: true)
        }else{
            let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "OrderListViewController") as! OrderListViewController
            self.navigationController?.pushViewController(ctrl, animated: true)
        }
    }
    
    // MARK:返回首页
    @IBAction func backRootClick(_ sender: UIButton) {
        self.tabBarController?.selectedIndex = 0
        self.navigationController?.popToRootViewController(animated: true)
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
